package sdc.com.petscalendar.adapters;

import java.io.File;
import java.util.List;

import sdc.com.petscalendar.R;
import sdc.com.petscalendar.objects.Veto;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VetoAdapter extends ArrayAdapter<Veto> {
	private OnEditListener editListener;

	public VetoAdapter(Context context, List<Veto> vetos) {
		super(context, R.layout.layout_veto_item, vetos);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		View v = convertView;
		if (v == null) {
			v = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE)).inflate(
					R.layout.layout_veto_item, parent, false);
			holder = new ViewHolder();
			holder.tvName = (TextView) v.findViewById(R.id.tv_name);
			holder.tvAdress = (TextView) v.findViewById(R.id.tv_address);
			holder.ivAvatar = (ImageView) v.findViewById(R.id.iv_avatar);
			holder.btnCall = v.findViewById(R.id.btn_call);
			holder.btnEdit = v.findViewById(R.id.btn_edit);
			holder.btnEmail = v.findViewById(R.id.btn_email);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		Veto veto = getItem(position);
		holder.tvName.setText(veto.getName());
		holder.tvAdress.setText(veto.getAddress() + "-" + veto.getCity());
		if (veto.getAvatar() != null && veto.getAvatar().length() > 0
				&& new File(veto.getAvatar()).exists()) {
			Bitmap bm = BitmapFactory.decodeFile(veto.getAvatar());
			holder.ivAvatar.setImageBitmap(bm);
		} else {
			holder.ivAvatar.setImageResource(R.drawable.default_avatar);
		}
		if (veto.getPhone() != null && veto.getPhone().length() > 0) {
			holder.btnCall.setEnabled(true);
			holder.btnCall.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent callIntent = new Intent(Intent.ACTION_CALL);
					callIntent.setData(Uri.parse("tel:"
							+ getItem(position).getPhone()));
					getContext().startActivity(callIntent);
				}
			});
		} else {
			holder.btnCall.setEnabled(false);
		}
		if (veto.getEmail() != null && veto.getEmail().length() > 0) {
			holder.btnEmail.setEnabled(true);
			holder.btnEmail.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri
							.fromParts("mailto", getItem(position).getEmail(),
									null));
					emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
					getContext().startActivity(
							Intent.createChooser(emailIntent, "Send email..."));
				}
			});
		} else {
			holder.btnEmail.setEnabled(false);
		}
		holder.btnEdit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (editListener != null) {
					editListener.onEditClicked(position);
				}
			}
		});

		return v;

	}

	public void setEditListener(OnEditListener editListener) {
		this.editListener = editListener;
	}

	private class ViewHolder {
		private TextView tvName, tvAdress;
		private View btnCall, btnEmail, btnEdit;
		private ImageView ivAvatar;
	}

	public interface OnEditListener {
		public void onEditClicked(int position);
	}

}
